package org.hvilela {
	import flash.net.URLRequest;	
	import flash.events.MouseEvent;	
	import flash.events.Event;	
	import flash.filters.GlowFilter;
	import flash.text.TextFormat;	
	import flash.display.DisplayObjectContainer;	
	import flash.text.TextField;	
	import flash.display.DisplayObject;	
	import flash.display.Sprite;
	import org.hvilela.Sphere;import flash.net.navigateToURL;	

	/**
	 * @author hvilela
	 * http://hvilela.com/
	 */
	public class TagCloud extends Sprite {
		
		private var tagCloud:Sphere;
		
		public function TagCloud() {
			tagCloud = new Sphere();
						
			tagCloud.x = stage.stageWidth / 2;
			tagCloud.y = stage.stageHeight / 2;

			tagCloud.addEventListener(Event.ADDED_TO_STAGE, ready);
			addChild(tagCloud);
		}
		
		private function ready(event : Event) : void {
			var labels:Array = [
				["Code", 1],
				["Action Script", 30],
				["IPhone", 27],
				["Java", 20],
				["Flash", 10],
				["Robocode", 15],
				["Webcam", 15],
				["Motion", 30],
				["Sphere", 20],
				["Blg", 17],
				["Tag", 25],
				["Macromedia", 10],
				["Blogger", 15],
				["Google", 20],
				["JVM", 10],
				["Google Wave", 5],
				["Star Wars", 15],
				["Action Script", 10],
				["IPhone", 15],
				["Java", 20],
				["Flash", 32],
				["Robocode", 10],
				["Webcam", 15],
				["Motion", 30],
				["Sphere", 5],
				["Blg", 17],
				["Tag", 5],
				["Macromedia", 10],
				["Blogger", 15],
				["Google", 20],
				["JVM", 10],
				["Google Wave", 13],
				["Star Wars", 30],
				["Action Script", 30],
				["IPhone", 27],
				["Java", 20],
				["Flash", 10],
				["Robocode", 15],
				["Webcam", 15],
				["Motion", 30],
				["Sphere", 25],
				["Blg", 17],
				["Tag", 28],
				["Macromedia", 10],
				["Blogger", 15],
				["Google", 20],
				["JVM", 10],
				["Google Wave", 17],
				["Star Wars", 15],
				["Action Script", 10],
				["IPhone", 15],
				["Java", 20],
				["Flash", 10],
				["Robocode", 10],
				["Webcam", 15],
				["Motion", 30],
				["Sphere", 12],
				["Blg", 17],
				["Tag", 8],
				["Macromedia", 10],
				["Blogger", 15],
				["Google", 20],
				["JVM", 10],
				["Google Wave", 23],
				["Star Wars", 27]
			];

			for (var i:uint = 0; i < labels.length; i++) {
				var tag : Sprite = new Sprite();
				tag.buttonMode = true;
				var label : TextField = new TextField();
				label.selectable = false;
				tag.addChild(label);
				label.text = labels[i][0];
				label.filters = [new GlowFilter(0xFFFFFF, 1, 4, 4, 3, 2)];

				var textFormat : TextFormat = new TextFormat();
				textFormat.font = "Arial";
				textFormat.size = labels[i][1];
				label.setTextFormat(textFormat);

				label.width = label.textWidth + 4;
				label.height = label.textHeight + 4;
				
				label.x = -label.width / 2;
				label.y = -label.height / 2;

				tag.addEventListener(MouseEvent.CLICK, openTag);

				tagCloud.addChild(tag);
			}
		}

		private function openTag(event : MouseEvent) : void {
			if (event.target instanceof TextField) {
				navigateToURL(new URLRequest("http://blog.hvilela.com/search/label/" + (event.target as TextField).text));
			}
		}
	}
}